<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
	<label for="tb-first">Input number between 0 and 999: </label>
	<input type="text" id="tb-first"/>
	<br />
	<a href="#" onclick="NumToWord()">To Word.</a>
	<div id="console">
	</div>
	<script src="scripts/js-console.js">	  
	</script>		  
	<script>
	
	function NumToWord() {
		var number = jsConsole.readFloat("#tb-first");
		var digits = ["zero" ,"one", "two", "three", "four", "five", "six", "seven", "eight","nine" ];
		var special = ["ten", "eleven", "twelve", "thirdtheen", "fourthen", "fiftheen", "sixtheen", "seventheen", "eightheen", "ninetheen" ];
		var dec = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];

		if (number >= 0 && number < 1000)
		{
			var digit = number % 10;
			var tens = parseInt(number / 10) % 10;
			var hundred = parseInt(number / 100) % 10;

			if (hundred !== 0)
			{
				jsConsole.write(digits[hundred] + " hundred ");
				if (tens !== 0 && tens !== 1 && number >= 20)
				{
					jsConsole.write("and " + dec[tens] + " ");
					if (digit != 0)
					{
						jsConsole.write(digits[digit] + " ");
					}
				}
				else if (tens === 1)
				{
					jsConsole.write("and " + special[digit]);
				}
				else
				{
					if (digit !== 0)
					{
						jsConsole.write("and " + digits[digit] + " ");
					}
				}
			}
			else
			{
                // hundreds = 0
                if (tens !== 0 && tens !== 1 && number >= 20)
                {
                	jsConsole.write(dec[tens] + " ");
                	if (digit !== 0)
                    {
                        jsConsole.write(digits[digit] + " ");
                    } 
                	
                }
                else if (tens === 1)
                {
                	jsConsole.write(special[digit]);
                }
                else
                {
                	jsConsole.write(digits[digit] + " ");
                }

            }
            jsConsole.writeLine(null);
        }
        else 
        {
        	jsConsole.writeLine("Out of range, try again!");
        }
    }

    </script>
</body>
</html>
